package com.kjtpay.design_pattern.iterator;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.adapter.AdapterActivity;

public class IteratorActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, IteratorActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iterator);
        setTitle("迭代器模式");

        TextView ticketTextView = findViewById(R.id.textViewTicket);

        ConcreteAggregate aggregate = new ConcreteAggregate();

        aggregate.add("大鸟");
        aggregate.add("小菜");
        aggregate.add("行李");
        aggregate.add("老外");
        aggregate.add("公交内部员工");
        aggregate.add("小偷");

        ConcreteIterator iterator = new ConcreteIterator(aggregate);
        String message = "";
        while (!iterator.isDone()) {
            message += iterator.currentItem() + " ====== 请买车票"  + "\n";
            iterator.next();
        }
        ticketTextView.setText(message);
    }
}